export const useDebounce = () => {
    let timerId = null;

    const debounce = (callback, delay) => {
        // 参数校验
        if (typeof callback !== 'function') {
            console.warn('debounce: callback 必须是一个函数');
            return;
        }

        if (typeof delay !== 'number' || delay < 0) {
            console.warn('debounce: delay 必须是非负数');
            delay = 300; // 默认值
        }

        // 清除之前的定时器
        if (timerId !== null) {
            clearTimeout(timerId);
            timerId = null;
        }

        // 设置新的定时器
        timerId = setTimeout(() => {
            callback();
            timerId = null;
        }, delay);
    };

    const clear = () => {
        if (timerId !== null) {
            clearTimeout(timerId);
            timerId = null;
        }
    };

    return {
        debounce,
        clear
    };
};
